#!/usr/bin/python
# -*- coding: UTF-8 -*-
from config import *
import pandas as pd
import pymongo;
client = pymongo.MongoClient(MONGO_URL);
MONGO_DB = "dzdp"
MONGO_TABLE = "food"
db = client[MONGO_DB]
import re
import matplotlib.pyplot as plot
import numpy
from utils.GwordClude import generate_cloud_word
import jieba
from io import StringIO
import numpy as np
#指定默认字体
plot.rcParams['font.sans-serif'] = ['SimHei']
plot.rcParams['font.family']='sans-serif'
def get_data():
    queryArgs = {}
    projectionFields = {'_id': False}  # 用字典指定
    result = db[MONGO_TABLE].find(queryArgs,projectionFields)
    return result


def showLDT():
    plot.style.use("ggplot")
    ablity_size = 4
    ability_labels = [u"口味", u"环境", u"服务"]

    player = {
        "0": [7.9,8.1,7.3],
        "1": [8.4,8.5,8.2],
        "2": [8.6,9.0,8.3],
        "3": [8.2,8.0,8.0],
        "4": [7.9,8.1,7.8]
    }

    threa = np.linspace(0, 2 * np.pi, 3, endpoint=False)
    np.append(threa, threa[0])

    # player["M"]=np.append(player['M'],player["M"][0])
    plot.figure(figsize=(6, 10))
    ax1 = plot.subplot(321, projection="polar")
    ax2 = plot.subplot(322, projection="polar")
    ax3 = plot.subplot(323, projection="polar")
    ax4 = plot.subplot(324, projection="polar")
    ax5 = plot.subplot(325, projection="polar")

    ax1.plot(threa, player["0"], "r")
    ax1.fill(threa, player["0"], "r", alpha=0.3)
    ax1.set_xticks(threa)
    ax1.set_xticklabels(ability_labels)
    ax1.set_title("知味观·味庄", color="r", size=13)

    ax2.plot(threa, player["1"], "g")
    ax2.fill(threa, player["1"], "g", alpha=0.3)
    ax2.set_xticks(threa)
    ax2.set_xticklabels(ability_labels)
    ax2.set_title("杭州酒家", color="g", size=13)

    ax3.plot(threa, player["2"], "b")
    ax3.fill(threa, player["2"], "b", alpha=0.3)
    ax3.set_xticks(threa)
    ax3.set_xticklabels(ability_labels)
    ax3.set_title("朴墅餐厅", position=(0.5, 1), color="b", size=13)

    ax4.plot(threa, player["3"], "y")
    ax4.fill(threa, player["3"], "y", alpha=0.3)
    ax4.set_xticks(threa)
    ax4.set_xticklabels(ability_labels)
    ax4.set_title("春暖花开", position=(0.5, 1), color="y", size=13)



    ax5.plot(threa, player["4"], "k")
    ax5.fill(threa, player["4"], "k", alpha=0.3)
    ax5.set_xticks(threa)
    ax5.set_xticklabels(ability_labels)
    ax5.set_title("新白鹿餐厅", position=(0.5, 1), color="k", size=13)
    plot.savefig("let.jpg")
    plot.show()


def main():
    data=pd.DataFrame(list(get_data()))

    data["price"]=data["price"].apply(filter_price)
    changeType(data)
    print(data.describe())
    data=data.sort_values(by="com_count",ascending=False)
    data=data.head(5)

    print(data)
    showLDT()


def show_location(data):
    data_csv = pd.DataFrame(data, columns=["shop_name", "location"])
    data_csv["location"] = data_csv["location"].apply(filter_location)
    data_csv.to_csv("location.csv", encoding="utf-8")
    print(data_csv)


def filter_location(str):
    return "浙江省 杭州市 "+str

def show(data_in):
    plot.style.use("seaborn-bright")
    bar_width = 0.3
    plot.figure(figsize=(16, 6))
    plot.title("杭州最受欢迎的5个小吃店")
    # plot.xticks(fontsize=7)
    plot.ylabel("评论次数")
    p2 = plot.bar(data_in["shop_name"], data_in["com_count"], bar_width)
    # plot.xticks(Rotation=45)
    plot.savefig("shop_com.png")
    plot.show()




def changeType(data):
    data["com_count"]=data["com_count"].astype(np.float)









def filter_price(salary):
    patter=re.compile("\S(\d+)")
    result=re.match(patter,salary)
    return (int(result.group(1)))




if __name__ == '__main__':
    main()
